//
//  AppDelegate.m
//  TreeMenuTest
//
//  Created by Etienne on 26/06/07.
//  Copyright (c) 2007 Etienne Samson. All rights reserved.
//

#import "TCUTree.h"
#import "TCUXMLTree.h"
#import "TCUXMLNode.h"

#import "NSString+TCUAdditions.h"

#import "TCUUserNotification.h"
#import "TCUBag.h"

#import "AppDelegate.h"
#import "TestExtendedTree.h"

@implementation AppDelegate

- (void) testTCUTree
{
    TCUTree  *node1 = [[TCUTree alloc] initWithObject:@"Test"];
    TCUTree  *node2 = [[TCUTree alloc] initWithObject:@"Test2"];
    TCUTree  *node3 = [[TCUTree alloc] initWithObject:@"Test3"];
    TCUTree  *node4 = [[TCUTree alloc] initWithObject:@"Test4"];
    TCUTree  *node5 = [[TCUTree alloc] initWithObject:@"Test5"];
    
    [node1 addChild:node2];
    [node1 addChild:node3];
    [node3 addChild:node4];
    [node4 addChild:node5];
    
    TCUTree *treeSimple = [TCUTree treeWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"treeSimpleData" ofType:@"plist"]];
    
    NSLog(@"%@", [treeSimple dictionaryRepresentation]);
    
    TCUTree *treeExtended = [TCUTree treeWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"treeExtendedData" ofType:@"plist"]];
    
    NSLog(@"%@", [treeExtended dictionaryRepresentation]);
    
    [node1 release];
    [node2 release];
    [node3 release];
    [node4 release];
    [node5 release];
    
    
    id tester = [[TestExtendedTree alloc] initWithObject:@"Test1" dataString:@"Test2"];
    
    NSLog(@"%@", [tester dictionaryRepresentation]);
    [tester release];
    
}

- (void) testTCUBag
{
    [TCUBag bagWithObjects:@"object1", @"object2", nil];
}

- (void) testTCUXMLNode
{
    id docNode;
    id subNode;
    id subNode2;
    id subNode3;
    
    docNode = [TCUXMLTree treeForStandaloneXMLDocumentWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.xml"] encoding:NSUTF8StringEncoding version:1.0];
    subNode = [TCUXMLTree xmlTreeWithNode:[TCUXMLNode elementNodeWithName:@"element" attributes:nil]];
    subNode2 = [TCUXMLTree xmlTreeWithNode:[TCUXMLNode elementNodeWithName:@"element2" attributes:nil]];
    subNode3 = [TCUXMLTree xmlTreeWithNode:[TCUXMLNode elementNodeWithName:@"elem3" attributes:nil]];
    
    [docNode addXMLChild:subNode];
    [subNode addXMLChild:subNode2];
    
    NSLog(@"%@", [NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding]);
    
    [NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding];

    [subNode addXMLChild:[TCUTree treeWithObject:[TCUXMLNode documentNodeWithURL:[NSURL URLWithString:@"p"] encoding:NSUTF8StringEncoding]]];
    
    NSLog(@"%@", [NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding]);
}

- (void) testUserNotification
{
    NSLog(@"%d", [TCUUserNotification displayAlert:@"Hey !" message:@"What are you looking at ?" timeout:2]);
    [TCUUserNotification displayNotice:@"Hmhmhm" message:@"Ahoy computer !" timeout:2];
    
    NSDictionary    *dict = [NSDictionary dictionaryWithObjectsAndKeys:
        @"Login form ?", kCFUserNotificationAlertHeaderKey,
        @"I need you login information to modify this item. If you have lost your password, feel free to click the checkbox !", kCFUserNotificationAlertMessageKey,
        [NSArray arrayWithObjects:@"Login name :", @"Password :", nil], kCFUserNotificationTextFieldTitlesKey,
        [NSArray arrayWithObjects:@"username", @"", nil], kCFUserNotificationTextFieldValuesKey,
        nil];
    
    TCUUserNotification *userNot = [[TCUUserNotification notificationWithDictionary:dict] retain];
    
    [userNot runInRunLoop:[NSRunLoop currentRunLoop] andDelegate:self];
    
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0f]];
    
    NSDictionary    *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
        @"I need you login information to modify this item. If you have lost your password, feel free to click the checkbox !", kCFUserNotificationAlertMessageKey,
        @"Oops, I forgot it !", kCFUserNotificationCheckBoxTitlesKey,
        nil];
    
    [userNot updateWithDictionary:dict2];
}

- (void) userNotification:(TCUUserNotification*)notification withResponseFlags:(int)flags
{
    static BOOL onlyOnce = NO;
    
    if (!onlyOnce) {
        NSLog(@"Called my callback ! %@", [notification responseDictionary]);
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"Preference ?", kCFUserNotificationAlertHeaderKey,
            @"Which of these item do you prefer ?", kCFUserNotificationAlertMessageKey,
            [NSArray arrayWithObjects:@"A coconut", @"A banana", nil], kCFUserNotificationCheckBoxTitlesKey,
            [NSArray arrayWithObjects:@"Pow !", @"Wizz !", @"Spleng !", nil], kCFUserNotificationPopUpTitlesKey,
            [NSNumber numberWithInt:2], kCFUserNotificationPopUpSelectionKey,
            nil];
    
        TCUUserNotification *not = [[TCUUserNotification notificationWithDictionary:dict] retain];
        [not runInRunLoop:[NSRunLoop currentRunLoop] andDelegate:self];
        onlyOnce = YES;
    }
    
    [notification release];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self testTCUBag];
    [self testTCUTree];
    [self testTCUXMLNode];
    [self testUserNotification];
}
@end